﻿using System;

// 定义一个接口，包含计算面积和判断合法性的方法
public interface IShape
{
    double CalculateArea();
    bool IsValid();
}

// 定义一个抽象类，实现 IShape 接口
public abstract class Shape : IShape
{
    public abstract double CalculateArea();
    public abstract bool IsValid();
}

// 长方形类
public class Rectangle : Shape
{
    public double Length { get; set; }
    public double Width { get; set; }

    public Rectangle(double length, double width)
    {
        Length = length;
        Width = width;
    }

    public override double CalculateArea()
    {
        return Length * Width;
    }

    public override bool IsValid()
    {
        return Length > 0 && Width > 0;
    }
}

// 正方形类
public class Square : Shape
{
    public double Side { get; set; }

    public Square(double side)
    {
        Side = side;
    }

    public override double CalculateArea()
    {
        return Side * Side;
    }

    public override bool IsValid()
    {
        return Side > 0;
    }
}

// 三角形类
public class Triangle : Shape
{
    public double Side1 { get; set; }
    public double Side2 { get; set; }
    public double Side3 { get; set; }

    public Triangle(double side1, double side2, double side3)
    {
        Side1 = side1;
        Side2 = side2;
        Side3 = side3;
    }

    public override double CalculateArea()
    {
        double s = (Side1 + Side2 + Side3) / 2;
        return Math.Sqrt(s * (s - Side1) * (s - Side2) * (s - Side3));
    }

    public override bool IsValid()
    {
        return Side1 > 0 && Side2 > 0 && Side3 > 0 &&
               Side1 + Side2 > Side3 &&
               Side1 + Side3 > Side2 &&
               Side2 + Side3 > Side1;
    }
}

class Program
{
    static void Main()
    {
        Console.WriteLine("请选择你要输入的图形：");
        Console.WriteLine("1. 长方形");
        Console.WriteLine("2. 正方形");
        Console.WriteLine("3. 三角形");
        Console.Write("请输入：");
        int choice = int.Parse(Console.ReadLine());

        if (choice == 1)
        {
            Console.Write("请输入长方形的长：");
            double length = double.Parse(Console.ReadLine());
            Console.Write("请输入长方形的宽：");
            double width = double.Parse(Console.ReadLine());
            Rectangle rectangle = new Rectangle(length, width);
            Console.WriteLine($"长方形是否合法: {rectangle.IsValid()}");
            Console.WriteLine($"长方形面积: {rectangle.CalculateArea()}");
        }
        else if (choice == 2)
        {
            Console.Write("请输入正方形的边长：");
            double side = double.Parse(Console.ReadLine());
            Square square = new Square(side);
            Console.WriteLine($"正方形是否合法: {square.IsValid()}");
            Console.WriteLine($"正方形面积: {square.CalculateArea()}");
        }
        else if (choice == 3)
        {
            Console.Write("请输入三角形的三边：");
            double side1 = double.Parse(Console.ReadLine());
            double side2 = double.Parse(Console.ReadLine());
            double side3 = double.Parse(Console.ReadLine());
            Triangle triangle = new Triangle(side1, side2, side3);
            Console.WriteLine($"三角形是否合法: {triangle.IsValid()}");
            Console.WriteLine($"三角形面积: {triangle.CalculateArea()}");
        }
        else
        {
            Console.WriteLine("输入错误！");

        }
    }
}
        